﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TicketMaster.Data;
using TicketMaster.Object;
using TicketMaster.Web.Models;
using Microsoft.AspNet.Identity;

namespace TicketMaster.Web.Controllers
{
    public class CommentsController : BaseController
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: Comments
        public ActionResult Index()
        {
            var comments = GetAllComments();
            return View(comments.ToList());
        }

        private IEnumerable<SubmittedCommentViewModel> GetAllComments()
        {
            var comments = this.Data.Comments
                .All()
                .Select(SubmittedCommentViewModel.FromComment)
                .ToList();

            return comments;
        }

        // GET: Comments/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Comment comment = Data.Comments.All().FirstOrDefault(x => x.Id == id);
            if (comment == null)
            {
                return HttpNotFound();
            }
            return View(comment);
        }

        // GET: Comments/Create
        public ActionResult Create()
        {

            return View();
        }

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Create(SubmittedCommentViewModel comment)
        {
            string currentUserId = User.Identity.GetUserId();
            ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);

            if (ModelState.IsValid)
            {
                Data.Comments.Add(new Comment()
                {
                    AuthorId = currentUserId,
                    Content = comment.Content,
                    RouteId = 3,
                    IsPositive = true,
                    Rate = comment.Rate,

                });
                Data.SaveChanges();
                return RedirectToAction("Index");
            }
            
    
            ViewBag.RouteId = comment.RouteId;
            return View(comment);
        }

       
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
